<?php

require_once 'domain/Thumb.php';
require_once(APPPATH.'libraries/MY_Model.php');

class M_channel_thumb extends MY_Model {

	function M_channel_thumb()
	{
		parent::MY_Model('live_thumbs','id');
	}

	/*****************************************************
	 * GENERAL
	 ****************************************************/

	protected function afromEntityToDb(Thumb $thumb){
		return array(
			'channel_suite_id'  => $thumb->channel_suite_id,
			'id'                => $thumb->thumb_id,
			'content'           => $thumb->content,
			'date'              => $this->getDateToDb($thumb->date)
                        );
	}

	protected function  afromDbToEntity(array $p_thumb) {
		$c = new Thumb();
		$c->channel_suite_id= $p_thumb['channel_suite_id'];
		$c->thumb_id        = $p_thumb['id'];
		$c->content         = $p_thumb['content'];
		$c->date            = $this->getDateFromDb($p_thumb['date']);
		return $c;
	}

	/*****************************************************
	 * ACCESO A DATOS
	 ****************************************************/

        function replaceOldestThumb(Thumb $thumb){
            //$this->db->select($this->TABLE_PK);
            $this->db->where("channel_suite_id",$thumb->channel_suite_id);
            $this->db->order_by("date");
            //$sql = 'select id from '.$this->TABLE_NAME.' order by date limit 1';
            $query = $this->db->get($this->TABLE_NAME);
            try{
                if($query->num_rows() < 4){
                    $this->create($thumb);
                }else{
                    $result_array = $query->result_array();
                    //$oldest_thumb = $this->fromDbToEntity($result_array[0]);
                    if( $result_array[0]['date'] < date_format($thumb->date,'Y-m-d H:i:s')){
                        //echo "\nreemplazando el último de fecha ". $result_array[0]['date'] .
                        //        " por el thumb de fecha " . $thumb->date->format('Y-m-d H:i:s') . "\n";
                        //$oldest_thumb->date = $thumb->date;
                        $result_array[0]['date'] = date_format($thumb->date,'Y-m-d H:i:s');
                        //$oldest_thumb->content = $thumb->content;
                        $result_array[0]['content'] = $thumb->content;

                        //$this->update($oldest_thumb);
                        $this->db->where($this->TABLE_PK,$result_array[0][$this->TABLE_PK]);
                        unset($result_array[0][$this->TABLE_PK]);
                        $this->db->update($this->TABLE_NAME,$result_array[0]);
                    }

                    //echo $this->db->last_query()."\n";
                }
            }catch(Exception $ex){
                  //echo $this->db->last_query();
                  throw $ex;
            }
        }

        function getRandThumb($channel_id){
            try{
                $this->db->where("channel_suite_id",$channel_id);
                $this->db->order_by("RAND()");
                $query = $this->db->get($this->TABLE_NAME,1);
                return $this->fromDbToEntity($query->result_array());
            }catch(Exception $ex){
                  echo $this->db->last_query();
                  throw $ex;
            }
        }
        
}

?>
